//
//  BaseInfoViewController.m
//  Yihubang
//
//  Created by xmy on 2016/12/2.
//  Copyright © 2016年 xmy. All rights reserved.
//

#import "BaseInfoViewController.h"
#import "TZImagePickerController.h"
typedef void (^RFimageSuccessDetailBlock)();
@interface BaseInfoViewController ()
@property (weak, nonatomic) IBOutlet UIImageView * sexImage;
@property (weak, nonatomic) IBOutlet UITextField * nametext;
@property (weak, nonatomic) IBOutlet UIButton    * seletedG;
@property (weak, nonatomic) IBOutlet UIButton    * selectedB;
@property (weak, nonatomic) IBOutlet UIButton *nextStepButton;
@property (nonatomic, strong) NSNumber *  sexNumber;
@property (nonatomic, strong) NSMutableDictionary * baseInfo;
@property (nonatomic, assign) BOOL freedomImg;
@end

@implementation BaseInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification"
                                              object:self.nametext];
    @weakify(self);
    self.sexImage.cornerRadius = 40.f;
    self.sexImage.masksToBounds =YES;
    [self.nextStepButton addTarget:self action:@selector(setBaseinfo) forControlEvents:UIControlEventTouchUpInside];
    [self.nametext.rac_textSignal subscribeNext:^(NSString * name){
        @strongify(self);
        if(name.length == 0){
            self.nextStepButton.enabled = NO;
            [self.nextStepButton setImage:[UIImage imageNamed:@"next_before"] forState:UIControlStateNormal];
        }else{
            self.nextStepButton.enabled = YES;
            [self.nextStepButton setImage:[UIImage imageNamed:@"next_complete"] forState:UIControlStateNormal];
        }
    }];
    [self.sexImage setTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self)
        [self selectHeadImg];
        
    }];
    [[self.seletedG rac_signalForControlEvents:UIControlEventTouchUpInside]
     subscribeNext:^(id x) {
         @strongify(self)
         self.seletedG.alpha = 1.f;
         self.selectedB.alpha = 0.4f;
         self.sexNumber = [NSNumber numberWithInt:2];
         if(!self.freedomImg){
            [self.sexImage setImage:[UIImage imageNamed:@"girl"]];
         }
     }];

    [[self.selectedB rac_signalForControlEvents:UIControlEventTouchUpInside]
     subscribeNext:^(id x) {
         @strongify(self)
         self.seletedG.alpha = 0.4f;
         self.selectedB.alpha = 1.0f;
         self.sexNumber = [NSNumber numberWithInt:1];
         if(!self.freedomImg){
             [self.sexImage setImage:[UIImage imageNamed:@"man"]];
         }
     }];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSMutableDictionary *)baseInfo{
    if(!_baseInfo){
        _baseInfo =[NSMutableDictionary dictionary];
    }
    return _baseInfo;
}
-(NSNumber *)sexNumber{
    if(!_sexNumber){
        _sexNumber =[NSNumber numberWithInt:2];
    }
    return _sexNumber;
}
-(void)selectHeadImg{
    @weakify(self);
    [LYAuthorizedMaster albumAuthorityCheckSuccess:^(void){
        @strongify(self);
        TZImagePickerController * imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
        [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
            @strongify(self);
            UIImage * image = photos[0];
            self.sexImage.image = image;
            self.freedomImg = YES;
            [self updateHeadImg:image Block:^(void){}];
        }];
        [self presentViewController:imagePickerVc animated:YES completion:nil];
    } fail:^(void){
        
    }];
}

-(void)setBaseinfo{
     @weakify(self);
    if(!self.freedomImg){
        [self updateHeadImg:self.sexImage.image Block:^(void){
            @strongify(self)
            [self saveBaseinfo];
        }];
    }else{
        [self saveBaseinfo];
    }
}
-(void)saveBaseinfo{
    @weakify(self);
    [self.baseInfo setObject:self.nametext.text forKey:@"nickname"];
    [self.baseInfo setObject:self.sexNumber forKey:@"sex"];
    [[[HttpManagerCenter sharedHttpManager] modifilrUserInfo:self.baseInfo resultClass:nil] subscribeNext:^(BaseModel *model){
        @strongify(self)
        if(model.code == 200){
            [self.userClient saveSetbaseInfo:[NSNumber numberWithBool:YES]];
            [self.userClient setUserInfo:self.baseInfo];
            [self hiddenHUDWithString:model.message error:NO];
            UIStoryboard *story = [UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]];
            UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"AlterSecurityCideViewController"];
            myView.params =@{@"identity":@"login"};
            [self pushViewController:myView animated:YES];
        }else{
            [self hiddenHUDWithString:model.message error:NO];
        }
    } error:^(NSError *error) {
    } completed:^{
    }];

}

-(void)updateHeadImg:(UIImage *)image Block:(RFimageSuccessDetailBlock)success;{
    [self showHUDWithString:@""];
     @weakify(self);
    [[[HttpManagerCenter sharedHttpManager] uploadImageOne:[image toUploadString]resultClass:nil] subscribeNext:^(BaseModel *model){
        @strongify(self);
        if(model.code==200){
//            self.sexImage.image = image;
            [self.baseInfo setObject:model.data[@"url"] forKey:@"portrait"];
            success();
            [self hiddenHUDWithString:@"上传成功" error:NO];
        }else{
            [self hiddenHUDWithString:@"请重新选择图片" error:NO];
        }
    }];
    

}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextFieldTextDidChangeNotification" object:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
